package com.asdan.seededu.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.asdan.seededu.entity.security.Teacher;
import com.asdan.seededu.service.TeacherService;
import com.asdan.seededu.service.impl.TeacherServiceImpl;
import com.asdan.seededu.util.Pager;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;

@Controller
@MenuSetting("menu-user")
@RequestMapping("teacher")
public class TeacherController {
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String addTeacher(String name,String schoolId ,String email ,String cellPhone ,Model model) {
		System.out.println(name + " "+email +" "
				+cellPhone+"  " +schoolId) ;
		TeacherService ts = new TeacherServiceImpl() ;
		if(ts.getTeacherByEmailOrID(email, -1)!=null){
			model.addAttribute("message", "增加失败，邮箱已注册！") ;
		}else{
		ts = new TeacherServiceImpl() ;
		Teacher stu = new Teacher(name, Integer.parseInt(schoolId), email , cellPhone ) ;
		Boolean isadd = ts.addTeacher(stu) ;
		if(isadd == true ){
			model.addAttribute("message", "增加成功！") ;
		}else{
			model.addAttribute("message", "增加失败，请重新输入！") ;
		}
		}
		return showTeaList("" ,"" ,"" ,"" ,"" ,model) ;
	}
	
	@RequestMapping(value="/showTeaList",method=RequestMethod.GET)
	public String showTeaList(String name,String schoolId ,String cellPhone ,String subProId,String pageNo,Model model ) {
		TeacherService ts = new TeacherServiceImpl() ;
		if(pageNo==null||pageNo.equals("")){
			pageNo = "1";
		}
		Pager<Teacher> page = ts.getTeachers(name, schoolId,  cellPhone, subProId, Integer.parseInt(pageNo));
		model.addAttribute("list", page.getDatas());
		model.addAttribute("pageNo",page.getOffset());
		model.addAttribute("pageCount", page.getTotalPage());
		model.addAttribute("totalCount",page.getTotal());
		model.addAttribute("size", page.getSize()) ;
		return "teacher";
	}
	
	@RequestMapping(value="/{tuid}/delTea",method=RequestMethod.GET)
	public String delStu(@PathVariable String tuid ,Model model) {
		TeacherService ts = new TeacherServiceImpl() ;
		boolean bool = ts.deleteTeacher(tuid) ;
		System.out.println(bool);
		if(bool){
			model.addAttribute("message" , "删除信息成功！") ;
		}else{
			model.addAttribute("message" , "删除信息失败！") ;
		}
		return this.showTeaList("", "", "", "", "", model) ;
	}
	
	
	@RequestMapping(value="/{tuid}/detail",method=RequestMethod.GET)
	public String getTeaByID(@PathVariable String tuid,Model model){
		TeacherService ts = new TeacherServiceImpl() ;
		Teacher tea = ts.getTeacherByEmailOrID(null, Integer.parseInt(tuid)) ;
		model.addAttribute("tea", tea) ;
		return "teadetail" ;
	}
	
	@RequestMapping(value="/{tuid}/reviseteas",method=RequestMethod.GET)
	public String updateteaByIDreviseteas(@PathVariable String tuid,Model model){
		TeacherService ts = new TeacherServiceImpl() ;
		Teacher tea = ts.getTeacherByEmailOrID(null, Integer.parseInt(tuid)) ;
		model.addAttribute("tea", tea) ;
		return "reviseteacher" ;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String update(Teacher tea ,Model model){
		TeacherService ss = new TeacherServiceImpl() ;
		boolean temp = ss.updateTeacher(tea) ;
		model.addAttribute("tea",new TeacherServiceImpl().getTeacherByEmailOrID("", tea.getTuid())) ;
		if(temp){
			model.addAttribute("message", "更改成功！") ;
		}else{
			model.addAttribute("message", "更改失败") ;
		}
		return "teadetail" ;
	}
	@RequestMapping(value="/showTeaBypage",method=RequestMethod.POST,produces="test/html;charset=UTF-8")
	@ResponseBody
	public String showStuBypage(String name,String schoolId  ,String cellPhone ,String subProId,String pageNo,Model model){
		TeacherService ts = new TeacherServiceImpl() ;
		if(pageNo==null||pageNo.equals("")){
			pageNo = "1";
		}
		Pager<Teacher> page = ts.getTeachers(name, schoolId,  cellPhone, subProId, Integer.parseInt(pageNo));
		System.out.println(JsonUtils.toJsonString(page));
		return JsonUtils.toJsonString(page) ;
	}	
}
